using UnityEngine;
using System;


public class SABattleCreautreManager : Singleton< SABattleCreautreManager > 
{
	[ HideInInspector ] public GameObject[] players = null;
	[ HideInInspector ] public GameObject[] playerPets = null;//new SABattleCreature[ GameDefine.MAX_BATTLE_PLAYER ];
	
	[ HideInInspector ] public GameObject[] enemyPlayers = null;
	[ HideInInspector ] public GameObject[] enemyPets = null;


	[ HideInInspector ] public SAMapGrid[] selfGrids = new SAMapGrid[ GameDefine.MAX_BATTLE_PLAYER ];//new SAMapGrid( 9 , 17 );
	[ HideInInspector ] public SAMapGrid[] selfPetGrids = new SAMapGrid[ GameDefine.MAX_BATTLE_PLAYER ];//new SAMapGrid( 9 , 17 );
	[ HideInInspector ] public SAMapGrid[] enemyGrids = new SAMapGrid[ GameDefine.MAX_BATTLE_PLAYER ];//new SAMapGrid( 10 , 2 );
	[ HideInInspector ] public SAMapGrid[] enemyPetGrids = new SAMapGrid[ GameDefine.MAX_BATTLE_PLAYER ];//new SAMapGrid( 10 , 2 );

	[ HideInInspector ] public GameObject creatureLayer = null;

	[ HideInInspector ] public int mainIndex = GameDefine.INVALID_ID;
	[ HideInInspector ] public int mainPetIndex = GameDefine.INVALID_ID;

	void Awake ()
	{
		mInstance = this;
		
	}

	void Start()
	{
		for ( int i = 0; i < GameDefine.MAX_BATTLE_PLAYER ; i++ ) 
		{
			selfGrids[ i ] = new SAMapGrid();
			selfPetGrids[ i ] = new SAMapGrid();
			enemyGrids[ i ] = new SAMapGrid();
			enemyPetGrids[ i ] = new SAMapGrid();
		}

		selfGrids[ 0 ].setValue( 9 , 16 );
		selfGrids[ 1 ].setValue( 8 , 16 );
		selfGrids[ 2 ].setValue( 10 , 16 );
		selfGrids[ 3 ].setValue( 7 , 16 );
		selfGrids[ 4 ].setValue( 11 , 16 );

		selfPetGrids[ 0 ].setValue( 9 , 13 );
		selfPetGrids[ 1 ].setValue( 7 , 13 );
		selfPetGrids[ 2 ].setValue( 11 , 13 );
		selfPetGrids[ 3 ].setValue( 5 , 13 );
		selfPetGrids[ 4 ].setValue( 13 , 13 );

		enemyGrids[ 0 ].setValue( 9 , 3 );
		enemyGrids[ 1 ].setValue( 8 , 3 );
		enemyGrids[ 2 ].setValue( 10 , 3 );
		enemyGrids[ 3 ].setValue( 7 , 3 );
		enemyGrids[ 4 ].setValue( 11 , 3 );
		
		enemyPetGrids[ 0 ].setValue( 9 , 6 );
		enemyPetGrids[ 1 ].setValue( 7 , 6 );
		enemyPetGrids[ 2 ].setValue( 11 , 6 );
		enemyPetGrids[ 3 ].setValue( 5 , 6 );
		enemyPetGrids[ 4 ].setValue( 13 , 6 );

		creatureLayer = GameObject.FindWithTag( "creatureLayer" );

		initCreatres();
	}

	public void clear()
	{
		players = null;
		playerPets = null;

		enemyPlayers = null;
		enemyPets = null;

		mainIndex = GameDefine.INVALID_ID;
		mainPetIndex = GameDefine.INVALID_ID;
	}


	public void initCreatres()
	{
		if ( GamePlayerData.instance.Team )
		{
		}
		else
		{
			players = new GameObject[ 1 ];
		}


		playerPets = new GameObject[ GameBattleData.instance.PetNum ];
		enemyPlayers = new GameObject[ GameBattleData.instance.EnemyNum ];
		enemyPets = new GameObject[ GameBattleData.instance.EnemyPetNum ];


		players[ 0 ] = SABattleCreature.CreateCreature();
		players[ 0 ].transform.parent = creatureLayer.transform;
		players[ 0 ].transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );

		SABattleCreature creature = players[ 0 ].GetComponent< SABattleCreature >();
		creature.creatuerData.copyDataFromPlayerData( GamePlayerData.instance.creatureData );
		creature.updateAction();
		creature.setOriginalDirection( (int)SAMapDirectionType.NORTH );
		creature.setOriginalPos( selfGrids[ 0 ].realPosX , selfGrids[ 0 ].realPosY , -1 , 1 );
		creature.initCreatureCommon( (byte)SABattleSessionSide.SELF , (byte)SABattleSessionRow.UNDER , (byte)0 );

		mainIndex = 0;
		mainPetIndex = GameDefine.INVALID_ID;

		int indexData = 0;

		for ( int i = 0 ; i < playerPets.Length ; i++ )
		{
			playerPets[ i ] = SABattleCreature.CreateCreature();
			playerPets[ i ].transform.parent = creatureLayer.transform;
			playerPets[ i ].transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );

			creature = playerPets[ i ].GetComponent< SABattleCreature >();

			creature.creatuerData = GameBattleData.instance.Data[ indexData ];
			creature.setActionID( creature.creatuerData.ActionID );
			creature.setOriginalDirection( (int)SAMapDirectionType.NORTH );
			creature.setOriginalPos( selfPetGrids[ i ].realPosX , selfPetGrids[ i ].realPosY , -1 , 1 );

			creature.initCreatureCommon( (byte)SABattleSessionSide.SELF , (byte)SABattleSessionRow.UP , (byte)i );
			indexData++;
		}

		for ( int i = 0 ; i < enemyPlayers.Length ; i++ )
		{
			enemyPlayers[ i ] = SABattleCreature.CreateCreature();
			enemyPlayers[ i ].transform.parent = creatureLayer.transform;
			enemyPlayers[ i ].transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );
			
			creature = enemyPlayers[ i ].GetComponent< SABattleCreature >();
			creature.initCreatureCommon( (byte)SABattleSessionSide.ENEMY , (byte)SABattleSessionRow.UNDER , (byte)i );

			creature.creatuerData = GameBattleData.instance.Data[ indexData ];
			creature.setActionID( creature.creatuerData.ActionID );
			creature.setOriginalDirection( (int)SAMapDirectionType.SOUTH );
			creature.setOriginalPos( enemyGrids[ i ].realPosX , enemyGrids[ i ].realPosY , 1 , -1 );

			if ( enemyPets.Length == 0 ) 
			{
				if ( enemyPlayers.Length < GameDefine.MAX_BATTLE_PLAYER )
				{
					creature.setOriginalPos( enemyPetGrids[ i ].realPosX , enemyPetGrids[ i ].realPosY , 1 , -1 );
				}
				else
				{
					if ( i >= GameDefine.MAX_BATTLE_PLAYER ) 
					{
						int ii = i - GameDefine.MAX_BATTLE_PLAYER;
						creature.setOriginalPos( enemyPetGrids[ ii ].realPosX , enemyPetGrids[ ii ].realPosY , 1 , -1 );
					}
				}
			}

			indexData++;
		}


		for ( int i = 0 ; i < enemyPets.Length ; i++ )
		{
			enemyPets[ i ] = SABattleCreature.CreateCreature();
			enemyPets[ i ].transform.parent = creatureLayer.transform;
			enemyPets[ i ].transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );

			creature = enemyPlayers[ i ].GetComponent< SABattleCreature >();
			creature.initCreatureCommon( (byte)SABattleSessionSide.ENEMY , (byte)SABattleSessionRow.UP , (byte)i );

			creature.creatuerData = GameBattleData.instance.Data[ indexData ];
			creature.setActionID( creature.creatuerData.ActionID );
			creature.setOriginalDirection( (int)SAMapDirectionType.SOUTH );
			creature.setOriginalPos( enemyPetGrids[ i ].realPosX , enemyPetGrids[ i ].realPosY , 1 , -1 );

			indexData++;
		}
	}

	public void enableEnemiesTargets( bool b )
	{
		for ( int i = 0; i < enemyPlayers.Length ; i++ ) 
		{
			SABattleCreature creature = enemyPlayers[ i ].GetComponent< SABattleCreature >();
			creature.enableTarget( creature.normalStatus != (int)SANormalStatusType.FALL ? b : false );
		}

		for ( int i = 0; i < enemyPets.Length ; i++ ) 
		{
			SABattleCreature creature = enemyPets[ i ].GetComponent< SABattleCreature >();
			creature.enableTarget( creature.normalStatus != (int)SANormalStatusType.FALL ? b : false );
		}

	}

	public SABattleSessionCommand getMainCommand()
	{
		SABattleCreature creature = players[ mainIndex ].GetComponent< SABattleCreature >();
		return creature.command;
	}

	public SABattleSessionCommand getMainPetCommand()
	{
		SABattleCreature creature = playerPets[ mainIndex ].GetComponent< SABattleCreature >();
		return creature.command;
	}

	public GameObject getCreature( byte s , byte r , byte i )
	{
		GameObject[] self1 = null;
		GameObject[] selfPet1 = null;
		GameObject[] enemy1 = null;
		GameObject[] enemyPet1 = null;

		if ( GameBattleData.instance.Side == (int)SABattleSessionSide.SELF )
		{
			self1 = players;
			selfPet1 = playerPets;
			enemy1 = enemyPlayers;
			enemyPet1 = enemyPets;
		}
		else
		{
			self1 = enemyPlayers;
			selfPet1 = enemyPets;
			enemy1 = players;
			enemyPet1 = playerPets;
		}

		if ( s == (int)SABattleSessionSide.SELF ) 
		{
			if ( r == (byte)SABattleSessionRow.UNDER )
			{
				return self1[ i ];
			}
			else
			{
				return selfPet1[ i ];
			}
		}
		else
		{
			if ( r == (byte)SABattleSessionRow.UNDER )
			{
				return enemy1[ i ];
			}
			else
			{
				return enemyPet1[ i ];
			}
		}
	}

//	public GameObject createPlayer( int id )
//	{
//		if ( PlayerDic.ContainsKey( id ) ) 
//		{
//			return PlayerDic[ id ];
//		}
//		
//		GameObject player = SAPlayer.CreatePlayer();
//		
//		if ( SAMapManager.instance != null && SAMapManager.instance.isLoead ) 
//		{
//			player.transform.parent = SAMapManager.instance.creatureLayer.transform;
//			player.transform.localScale = new Vector3( 1.0f , 1.0f , 1.0f );
//		}
//		else
//		{
//			player.transform.parent = transform;
//		}
//		
//		PlayerDic.Add( id , player );
//		
//		return player;
//	}

}

